package com.kx.center.util;

import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kx.center.enumerate.MessageCode;

// 处理返回信息
public class Response {

	
	protected static final String SUCCESS = "success";
	protected static final String WARNING = "warning";
	protected static final String ERROR = "error";
	
	public static String success() {
		return success(null, null, JsonInclude.Include.ALWAYS);
	}

	public static String success(String message) {
		return success(message, null, JsonInclude.Include.ALWAYS);
	}
	
	public static String success(HashMap<String, Object> args) {
		return success(null, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String success(String message, HashMap<String, Object> args) {
		return success(message, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String success(JsonInclude.Include incl) {
		return success(null, null, incl);
	}

	public static String success(String message, JsonInclude.Include incl) {
		return success(message, null, incl);
	}
	
	public static String success(HashMap<String, Object> args, JsonInclude.Include incl) {
		return success(null, args, incl);
	}
	
	public static String success(String message, HashMap<String, Object> args, JsonInclude.Include incl) {
		return message(SUCCESS, message, args, incl);
	}
	
	public static String warning() {
		return warning(MessageCode.UNKNOWN, null, null, JsonInclude.Include.ALWAYS);
	}
	
	public static String warning(MessageCode code) {
		return warning(code, null, null, JsonInclude.Include.ALWAYS);
	}

	public static String warning(String message) {
		return warning(message, null, JsonInclude.Include.ALWAYS);
	}

	public static String warning(MessageCode code, String message) {
		return warning(code, message, null, JsonInclude.Include.ALWAYS);
	}
	
	public static String warning(HashMap<String, Object> args) {
		return warning(MessageCode.UNKNOWN, null, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String warning(MessageCode code, HashMap<String, Object> args) {
		return warning(code, null, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String warning(String message, HashMap<String, Object> args) {
		return warning(message, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String warning(MessageCode code, String message, HashMap<String, Object> args) {
		return warning(code, message, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String warning(JsonInclude.Include incl) {
		return warning(MessageCode.UNKNOWN, null, null, incl);
	}
	
	public static String warning(MessageCode code, JsonInclude.Include incl) {
		return warning(code, null, null, incl);
	}

	public static String warning(String message, JsonInclude.Include incl) {
		return warning(message, null, incl);
	}

	public static String warning(MessageCode code, String message, JsonInclude.Include incl) {
		return warning(code, message, null, incl);
	}
	
	public static String warning(HashMap<String, Object> args, JsonInclude.Include incl) {
		return warning(MessageCode.UNKNOWN, null, args, incl);
	}
	
	public static String warning(MessageCode code, HashMap<String, Object> args, JsonInclude.Include incl) {
		return warning(code, null, args, incl);
	}
	
	public static String warning(String message, HashMap<String, Object> args, JsonInclude.Include incl) {
		return message(WARNING, message, args, incl);
	}
	
	public static String warning(MessageCode code, String message, HashMap<String, Object> args, JsonInclude.Include incl) {
		return message(code, WARNING, message, args, incl);
	}
	
	public static String error() {
		return error(null, null, null, JsonInclude.Include.ALWAYS);
	}
	
	public static String error(MessageCode code) {
		return error(code, null, null, JsonInclude.Include.ALWAYS);
	}

	public static String error(String message) {
		return error(null, message, null, JsonInclude.Include.ALWAYS);
	}

	public static String error(MessageCode code, String message) {
		return error(code, message, null, JsonInclude.Include.ALWAYS);
	}

	public static String error(HashMap<String, Object> args) {
		return error(MessageCode.UNKNOWN, args, JsonInclude.Include.ALWAYS);
	}

	public static String error(MessageCode code, HashMap<String, Object> args) {
		return error(code, null, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String error(JsonInclude.Include incl) {
		return error(null, null, null, incl);
	}
	
	public static String error(MessageCode code, JsonInclude.Include incl) {
		return error(code, null, null, incl);
	}

	public static String error(String message, JsonInclude.Include incl) {
		return error(null, message, null, incl);
	}

	public static String error(MessageCode code, String message, JsonInclude.Include incl) {
		return error(code, message, null, incl);
	}

	public static String error(HashMap<String, Object> args, JsonInclude.Include incl) {
		return error(MessageCode.UNKNOWN, args, incl);
	}

	public static String error(MessageCode code, HashMap<String, Object> args, JsonInclude.Include incl) {
		return error(code, null, args, incl);
	}

	public static String error(String message, HashMap<String, Object> args) {
		return error(message, args, JsonInclude.Include.ALWAYS);
	}

	public static String error(MessageCode code, String message, HashMap<String, Object> args) {
		return error(code, message, args, JsonInclude.Include.ALWAYS);
	}

	public static String error(String message, HashMap<String, Object> args, JsonInclude.Include incl) {
		return message(ERROR, message, args, incl);
	}

	public static String error(MessageCode code, String message, HashMap<String, Object> args, JsonInclude.Include incl) {
		return message(code, ERROR, message, args, incl);
	}
	
	public static String message(String status,String message){
		return message(null, status, message, null, JsonInclude.Include.ALWAYS);
	}
	
	public static String message(MessageCode code, String status,String message){
		return message(code, status, message, null, JsonInclude.Include.ALWAYS);
	}
	
	public static String message(String status, String message, HashMap<String, Object> args){
		return message(null, status, message, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String message(MessageCode code, String status, String message, HashMap<String, Object> args){
		return message(code, status, message, args, JsonInclude.Include.ALWAYS);
	}
	
	public static String message(String status,String message, JsonInclude.Include incl){
		return message(null, status, message, null, incl);
	}
	
	public static String message(MessageCode code, String status,String message, JsonInclude.Include incl){
		return message(code, status, message, null, incl);
	}
	
	public static String message(String status, String message, HashMap<String, Object> args, JsonInclude.Include incl){
		return message(MessageCode.UNKNOWN, status, message, args, incl);
	}
	
	public static String message(MessageCode code, String status, String message, HashMap<String, Object> args, JsonInclude.Include incl){
		HashMap<String, Object> map;

		if(args == null) {
			map = new HashMap<String,Object>();
		} else {
			map = args;
		}
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(incl);

		try {
			map.put("status", status);

			
			if(StringUtils.isNotBlank(message)) {
				map.put("msg", message);
			} else if(code != null) {
				map.put("msg", code.getMessage());
			}
			
			if(code != null) {
				map.put("code", code.getValue());
			}
			
			return mapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return "{\"status\":\"error\"}";
		}
	}
}
